<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/23
 * Time: 16:40
 */

namespace HiCommon\Repository;

use HiCommon\Model\RoadRescueOrder;
class RoadRescueOrderRepository extends BaseRepository
{


    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list( array $arr_search = [] , array $arr_option = [] )
    {
        return RoadRescueOrder::where(  function ($query) use($arr_search) {
            if( !empty($arr_search['pid']) ){
                $query->where('pid',$arr_search['pid']);
            }
            if( !empty( $arr_search['pay_status'] ) ){
                if( $arr_search['pay_status'] == 1 ){  //已支付
                    $query->where('pay_status',$arr_search['pay_status'] )->whereIn('status' , [ 2,21,22,3]);
                }else{   //未支付
                    $query->where(['pay_status' => $arr_search['pay_status']  ] );
                }
            }
            if( !empty( $arr_search['act'] ) ){
                if( $arr_search['act'] == 1 ){  //待救援
                    $query->where(['pay_status'=>1 , 'status'=>1 ]);
                }else{   //代付款
                    $query->where(['pay_status' => 2, 'status'=>0 ] );
                }
            }


            if( !empty($arr_search['status']) ){
                $query->where('status',$arr_search['status']);
            }

            if( !empty($arr_search['order_sn']) ){
                $query->where('order_sn','like' , '%'.$arr_search['order_sn'].'%');
            }
        })->commonpage($arr_option);
    }

    /**
     * 通过where分页获取数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all($where, $map)
    {
        return RoadRescueOrder::where($where)->get($map)->toarray();

    }

    /**
     * 通过where分页获取数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all2($where, $arr_option = [])
    {
        return RoadRescueOrder::where($where)->CommonGet($arr_option);

    }


    /**
     *  根据商铺分组获取数据
     *
     **/
    public function get_shop_list(array $arr_option )
    {
        $raw = \DB::raw(" shop_id ");
        return RoadRescueOrder::select($raw)->where($arr_option['where'])->with($arr_option['with'])->groupBy('shop_id')->get();
    }


    /**
     * 分页获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all_wherein(array $ids ,  array $arr_option = [])
    {
        return RoadRescueOrder::whereIn('shop_id' , $ids )->commonget($arr_option);
    }



    /**
     * 个人中心统计道路救援订单
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_whereIn($arr_where)
    {
        return RoadRescueOrder::where($arr_where)->whereIn('status',['1','2'] )->count();
    }


    /**
     * 根据条件统计数量
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_where($arr_where)
    {
        return RoadRescueOrder::where($arr_where)->count();
    }


    /**
     * 根据条件统计总和
     *
     * @param array $arr_where
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_sum_by_where($arr_where = [], $s_field = 'mark')
    {
        return RoadRescueOrder::where($arr_where)->sum($s_field);
    }

    /**
     * 添加一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one($arr_input)
    {
        return RoadRescueOrder::create($arr_input);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id, $arr_option = [])
    {
        return RoadRescueOrder::commonfind($n_id, $arr_option);
    }


    /**
     * 根据条件查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one_where( array $where, array $arr_option = [] )
    {
        return RoadRescueOrder::where($where)->CommonFirst( $arr_option);
    }


    /**
     *  根据条件查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one_where_user( array $where, array $arr_option = [] )
    {
        return RoadRescueOrder::where($where)->whereNotIn('status',['3','4','22'] )->CommonFirst( $arr_option);
    }




    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        return RoadRescueOrder::where('id', $arr_input['id'])->update($arr_input);
    }


    /**
     * 根据条件更新数据
     */
    public function edit_one_where(array $map, array $arr_input)
    {
        return RoadRescueOrder::where($map)->update($arr_input);
    }


    /**
    * 根据条件更新数据
    */
    public function edit_one_whereIn(array $map, array $arr_input)
    {
        return RoadRescueOrder::whereIn($map['key'] , $map['value'])->update($arr_input);
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one(int $n_id)
    {
        return RoadRescueOrder::where('id', $n_id)->commondelete();
    }

    //根据条件删除数据
    public function delete_where_one( array $where = [] )
    {
        return RoadRescueOrder::where( $where )->commondelete( true );
    }


    /**
     * 修改一条数据状态
     *
     * @param int $n_id
     * @param int $n_state
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state(int $n_id, int $n_state)
    {
        return RoadRescueOrder::where('id', $n_id)->update(['state' => $n_state]);

    }

    /**
     * 字段自增
     *
     * @param int $n_id
     * @param string $s_field
     * @param int $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num($n_id, $s_field = 'commend_num', $n_step = 1)
    {
        return RoadRescueOrder::where('id', $n_id)->increment($s_field, $n_step);
    }

    /**
     * 字段自减
     *
     * @param int $n_id
     * @param string $s_field
     * @param int $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num($n_id, $s_field = 'commend_num', $n_step = 1)
    {
        return RoadRescueOrder::where('id', $n_id)->decrement($s_field, $n_step);
    }

    /**
     *  根据条件获取数量（开发票时检测当前订单是否可以开票）
     *  hinq 2020 6 15
     *
     **/
    public function get_count_whereIn( array $arr_whereIn = ['key'=>'id','value'=>[]]){
        return RoadRescueOrder::whereIn($arr_whereIn['key'], $arr_whereIn['value'])->whereIn('is_invoice',[1,2])->count();
    }

}